package projetobancodedados;

import java.sql.*;
import java.util.ArrayList;

/**
 */
public class DisciplinaControle {
    connectionFactory cnFact;
    Connection con;
    public DisciplinaControle()
    {
        cnFact = new connectionFactory();
        con = cnFact.getConnection();
    }
    public void addDisciplina(Disciplina d)
    {
        try{
            String cod = d.getCod();
            String nome = d.getNome();
            String curso = d.getCurso();
            String prof = d.getProfessor();
        
            String querry = "INSERT INTO disciplina(cod_disciplina, nome_disciplina, curso, professor) VALUES (?, ?, ?, ?)";
            PreparedStatement ps = con.prepareStatement(querry);
            ps.setString(1, cod);
            ps.setString(2, nome);
            ps.setString(3, curso);
            ps.setString(4, prof);
            ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public ArrayList listaDisciplinas(){
       ArrayList<Disciplina> disciplinas = new ArrayList();
       Disciplina d;
       try{
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("select disciplina.cod_disciplina, disciplina.nome_disciplina, disciplina.curso, "
                    + "professor.nome as professor from disciplina"
                    + " inner join professor on disciplina.professor= professor.doc_professor");
            while(rs.next()){
                d = new Disciplina(rs.getString("cod_disciplina"), rs.getString("nome_disciplina"
                        + ""), rs.getString("curso"), rs.getString("professor"));
                disciplinas.add(d);
            }
       }catch(Exception e){
           e.printStackTrace();
       }
       return disciplinas;
    }
    public void closeConnection()
    {
        try{
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
